package com.ruicar.afs.cloud.gateway.components;

import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.ClientTypeUtil;
import com.ruicar.afs.cloud.gateway.handler.loginprocess.LoginProcess;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@AllArgsConstructor
public class LoginProcessHolder {
    private static final Map<ClientTypeUtil.ClientTypeEnum, LoginProcess> LOGIN_PROCESS_MAP= new HashMap<>();
    private final List<LoginProcess> loginProcesses;

    @PostConstruct
    public void init(){
        loginProcesses.stream().forEach(loginProcess -> {
            loginProcess.supportClientType().stream().forEach(clientType->{
                LOGIN_PROCESS_MAP.put(clientType,loginProcess);
            });
        });
    }

    public static LoginProcess getLoginProcess(ClientTypeUtil.ClientTypeEnum clientTypeEnum){
        if(LOGIN_PROCESS_MAP.containsKey(clientTypeEnum)){
            return LOGIN_PROCESS_MAP.get(clientTypeEnum);
        }else{
            throw new AfsBaseException(CommonConstants.FAIL,"不允许登录");
        }
    }
}
